package com.tboss.template.common.dwr;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.tboss.template.sample.dwr.treeviewDao.TreeviewDao;
import com.tboss.template.sample.dwr.treeviewService.TreeviewService;
import com.tboss.template.sample.dwr.treeviewType.TreeviewServiceType;

@RemoteProxy(name="treeviewSample")
public class TreeviewSample {
	
	@Autowired TreeviewDao treeviewDao;
	@Value("#{funcs['treeview']}") private String treeviewPackage;
	@Value("#{sysDataPath['system.data.path']}") private String sysDataPath;
	
	@RemoteMethod
	public String treeMenuList(HashMap<String, String> opts) 
			throws JsonGenerationException, JsonMappingException, IOException{
			
		opts.put("treeDataFile", this.sysDataPath + opts.get("treeDataFile"));

		return this.treeMenuList(opts.get("callback"), opts);
	}
	
	@RemoteMethod
	private String treeMenuList(String type, HashMap<String, String> opts) 
			throws JsonGenerationException, JsonMappingException, IOException{

		return this.treeViewBuild(TreeviewServiceType.get(type), opts);
	}
	
	@SuppressWarnings("unchecked")
	private String treeViewBuild(TreeviewServiceType type, HashMap<String, String> opts){
		StringBuffer rtnValue = null;
		String clazz = this.treeviewPackage + type.getValue();
		
		try {
			Class<TreeviewService> service = (Class<TreeviewService>) Class.forName(clazz);
			
			if(type == TreeviewServiceType.JD || type == TreeviewServiceType.HD){
				rtnValue = service.newInstance().build(opts, this.treeviewDao);
			}else{
				rtnValue = service.newInstance().build(opts);
			}
			
		} catch (ClassNotFoundException e) { e.printStackTrace();
		} catch (InstantiationException e) { e.printStackTrace();
		} catch (IllegalAccessException e) { e.printStackTrace();
		} finally {
			if(rtnValue == null){
				rtnValue = new StringBuffer();
				StringBuffer err = new StringBuffer();
				err.append("<font style='color:red;'>");
				err.append("데이터를 정상적으로 가져오지 못했습니다.<br/><br/>");
				err.append("관리자에게 문의 하세요.");
				err.append("</font>");
				
				if(type == TreeviewServiceType.JD || type == TreeviewServiceType.JF || type == TreeviewServiceType.JM){
					List<String> list = new LinkedList<String>();
					list.add(err.toString());
					
					Map<String, List<String>> menu = new HashMap<String, List<String>>();
					menu.put("error", list);

					try {
						rtnValue.append(new ObjectMapper().writeValueAsString(menu));
						
					} catch (JsonGenerationException e) { e.printStackTrace();
					} catch (JsonMappingException e) { e.printStackTrace();
					} catch (IOException e) { e.printStackTrace();
					}
					
				}else{
					rtnValue.append(err.toString());
				}
				
				return rtnValue.toString();
			}
		}
		
		return rtnValue.toString();
	}
	
}
